package com.umerclass.ui.anmo.three;

import android.app.Dialog;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.umerclass.R;
import com.umerclass.bean.anmo.three.DeviceBean;
import com.umerclass.bean.anmo.three.DeviceListBean;
import com.umerclass.bean.anmo.three.DeviceSetBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.okhttp.OkEntityListRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import com.umerclass.ui.anmo.three.adapter.JiQiSetButtomAdapter;
import com.umerclass.ui.anmo.three.adapter.JiQiSetTopAdapter;
import com.umerclass.utils.Constants;
import com.umerclass.utils.screen.ScreenUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 机器设置
 */
public class JiQiSheActivity extends BaseActivity implements OptListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.recycleview2)
    RecyclerView recycleview2;
    private List<DeviceBean> deviceBeans = new ArrayList<>();
    private int page = 1;
    private int rows = 10;//单页数量
    private JiQiSetTopAdapter jiQiSetTopAdapter;
    private boolean isLoading = false;
    private boolean loadMore = true;
    //设备套餐列表
    private List<DeviceSetBean> deviceSetBeanList = new ArrayList<>();
    private JiQiSetButtomAdapter jiQiSetButtomAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_jiqi_set;
    }

    @Override
    public void init() {

        todo();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        jiQiSetTopAdapter = new JiQiSetTopAdapter(this, deviceBeans,
                R.layout.activity_anmo_jiqi_set_item_top, this);
        recycleview.setLayoutManager(layoutManager);
        recycleview.setAdapter(jiQiSetTopAdapter);
        recycleview.addOnScrollListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //ToastUtils.showShort("加载更多");
                if (loadMore) {
                    ++page;
                    getData();
                }
            }
        });

        //底部套餐
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        jiQiSetButtomAdapter = new JiQiSetButtomAdapter(this, deviceSetBeanList,
                R.layout.activity_anmo_jiqi_set_item_buttom, this);
        recycleview2.setLayoutManager(layoutManager2);
        recycleview2.setAdapter(jiQiSetButtomAdapter);
    }

    @Override
    public void loadData() {
        getData();
    }

    @OnClick({R.id.linear_back,R.id.btn_change})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.btn_change:
                createDialogOne();
                break;
                default:break;
        }
    }

    private void getData() {
        OkEntityListRequest<DeviceListBean> request = new OkEntityListRequest<>(R.id.myDeviceList,
                Constants.myDeviceList, DeviceListBean.class);
        request.addParams("userId", "");
        request.addParams("page", page);
        request.addParams("rows", rows);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    @Override
    public void onOptClick(View view, Object object) {

        switch (view.getId()) {
            case R.id.linear:
                int position = (int) object;
                boolean select = deviceBeans.get(position).isSelect();
                if (select) {
                    return;
                } else {
                    for (int i = 0; i < deviceBeans.size(); i++) {
                        if (position == i) {
                            deviceBeans.get(position).setSelect(true);
                        } else {
                            deviceBeans.get(i).setSelect(false);
                        }
                    }
                    jiQiSetTopAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.tvGaiBian:
                int index = (int) object;
                tips = deviceSetBeanList.get(index).getName()+"修改";
                if(dialogTwo==null){
                    createDialogTwo();
                }else {
                    price1Two = "";
                    price2Two = "";
                    totalNum = "";
                    tv_tips_two.setText(tips);
                    editText1Two.setText(price1Two);
                    editText2Two.setText(price2Two);
                    editText3Two.setText(totalNum);
                    if(!dialogTwo.isShowing()){
                        dialogTwo.show();
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what) {
            case R.id.myDeviceList:
                DeviceListBean deviceListBean = (DeviceListBean) t.getT();
                List<DeviceBean> infos = deviceListBean.getList();
                if (infos != null && infos.size() > 0) {

                } else {
                    loadMore = false;
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void todo() {

        for (int i = 0; i < 6; i++) {
            DeviceBean deviceBean = new DeviceBean();
            if (i == 0) {
                deviceBean.setSelect(true);
            }
            deviceBean.setDeviceNo("DH5614" + i);
            deviceBeans.add(deviceBean);
        }

        for (int i = 0; i < 6; i++) {
            DeviceSetBean deviceSetBean = new DeviceSetBean();
            deviceSetBean.setName("套餐"+ i);
            deviceSetBeanList.add(deviceSetBean);
        }
    }

    private Dialog dialogOne;
    private Button btnSure;
    private ImageView imageView;
    private EditText editText1,editText2;
    private String price1 = "";
    private String price2 = "";
    private Dialog createDialogOne() {
        if (dialogOne == null) {
            dialogOne = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.activity_device_set_one_dialog, null);
            imageView = (ImageView) inflate.findViewById(R.id.image);
            editText1 = (EditText) inflate.findViewById(R.id.et_one);
            editText2 = (EditText) inflate.findViewById(R.id.et_two);
            btnSure = (Button) inflate.findViewById(R.id.btn_sure);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(dialogOne.isShowing()){
                        dialogOne.dismiss();
                    }
                }
            });
            editText1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        price1 = "";
                    }else {
                        price1 = s.toString();
                    }

                    if(TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2)){
                        btnSure.setBackgroundResource(R.drawable.shape_one_normal);
                    }else {
                        btnSure.setBackgroundResource(R.drawable.shape_one_press);
                    }
                }
            });
            editText2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        price2 = "";
                    }else {
                        price2 = s.toString();
                    }

                    if(TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2)){
                        btnSure.setBackgroundResource(R.drawable.shape_one_normal);
                    }else {
                        btnSure.setBackgroundResource(R.drawable.shape_one_press);
                    }
                }
            });
            btnSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!TextUtils.isEmpty(price1) && !TextUtils.isEmpty(price2)){
                        dialogOne.dismiss();

                    }
                }
            });
            //将布局设置给Dialog
            dialogOne.setContentView(inflate);
            Window dialogWindow = dialogOne.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.CENTER);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogOne.show();
        }
        return dialogOne;
    }


    //============================================================
    private Dialog dialogTwo;
    private Button btnSureTwo;
    private ImageView imageViewTwo;
    private EditText editText1Two,editText2Two,editText3Two;
    private String price1Two = "";
    private String price2Two = "";
    private String totalNum = "";
    private String tips = "";
    private TextView tv_tips_two;

    private Dialog createDialogTwo() {
        if (dialogTwo == null) {
            dialogTwo = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.activity_device_set_two_dialog, null);
            imageViewTwo = (ImageView) inflate.findViewById(R.id.image);
            tv_tips_two = (TextView) inflate.findViewById(R.id.tv_tips_two);
            editText1Two = (EditText) inflate.findViewById(R.id.et_one);
            editText2Two = (EditText) inflate.findViewById(R.id.et_two);
            editText3Two = (EditText) inflate.findViewById(R.id.et_three);
            tv_tips_two.setText(tips);
            editText1Two.setText(price1Two);
            editText2Two.setText(price2Two);
            editText3Two.setText(totalNum);

            btnSureTwo = (Button) inflate.findViewById(R.id.btn_sure);
            imageViewTwo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(dialogTwo.isShowing()){
                        dialogTwo.dismiss();
                    }
                }
            });
            editText1Two.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        price1 = "";
                    }else {
                        price1 = s.toString();
                    }

                    if(TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2)|| TextUtils.isEmpty(totalNum)){
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_normal);
                    }else {
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_press);
                    }
                }
            });
            editText2Two.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        price2 = "";
                    }else {
                        price2 = s.toString();
                    }

                    if(TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2)|| TextUtils.isEmpty(totalNum)){
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_normal);
                    }else {
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_press);
                    }
                }
            });
            editText3Two.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(TextUtils.isEmpty(s.toString())){
                        totalNum = "";
                    }else {
                        totalNum = s.toString();
                    }

                    if(TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2)|| TextUtils.isEmpty(totalNum)){
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_normal);
                    }else {
                        btnSureTwo.setBackgroundResource(R.drawable.shape_one_press);
                    }
                }
            });
            btnSureTwo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!TextUtils.isEmpty(price1Two) && !TextUtils.isEmpty(price2Two)){
                        dialogTwo.dismiss();

                    }
                }
            });
            //将布局设置给Dialog
            dialogTwo.setContentView(inflate);
            Window dialogWindow = dialogTwo.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.CENTER);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogTwo.show();
        }
        return dialogTwo;
    }
}
